class QueryEntity {
    constructor({
                    id = '',
                    idcId = '',
                    startTime = '',
                    endTime = '',
                    idAction = SearchActionConstant.ACCURATE,
                    idcIdAction = SearchActionConstant.ACCURATE,
                    valueStart = '',
                    houseId = '',
                    subtype = '',
                    operationType = '',
                } = {}) {
        this.id = id;
        this.idcId = idcId;
        this.startTime = startTime;
        this.endTime = endTime;
        this.idAction = idAction;
        this.idcIdAction = idcIdAction;
        this.valueStart = valueStart;
        this.houseId = houseId;
        this.subtype = subtype;
        this.operationType = operationType;
    }
}

const logMap = new Map().set(0, '不记录').set(1, '记录')

const reportMap = new Map().set(0, '不上传').set(1, '上传')

const operationTypeMap = new Map().set(0, '新增').set(1, '删除')


const subTypeFormat = new Map().set(1, '域名')
    .set(2, 'URL')
    .set(3, '关键字')
    .set(4, '源IP地址')
    .set(5, '目的IP地址')
    .set(6, '源端口')
    .set(7, '目的端口')
    .set(8, '传输层协议')

const insSourceMap = new Map().set(1, 'SMMS').set(2, 'ISMS')

new BasicTableVue({
    data: function () {
        return {
            api: insFilterApi,
            columns: [
                new TableColumn({
                    type: 'selection',
                    width: 30,
                    align: 'center',
                }),
                new TableColumn({
                    prop: 'id',
                    title: '指令ID',
                }),
                new TableColumn({
                    prop: 'idcId',
                    title: '经营者ID',
                }),
                new TableColumn({
                    prop: 'insSource',
                    title: '指令源',
                    formatter: this.insSourceFormat,
                }),
                new TableColumn({
                    prop: 'houseName',
                    title: '机房范围',
                }),
                new TableColumn({
                    prop: 'reason',
                    title: '过滤原因',
                }),
                new TableColumn({
                    prop: 'log',
                    title: '记录',
                    formatter: this.logFormat,
                }),
                new TableColumn({
                    prop: 'report',
                    title: '上报',
                    formatter: this.reportFormat,
                }),
                new TableColumn({
                    prop: 'level',
                    title: '优先级',
                    formatter: this.levelFormat,
                }),
                new TableColumn({
                    prop: 'effectTime',
                    title: '生效时间',
                    formatter: TableColumn.datetimeFormat,
                }),
                new TableColumn({
                    prop: 'expiredTime',
                    title: '过期时间',
                    formatter: TableColumn.datetimeFormat,
                }),
                new TableColumn({
                    prop: 'owner',
                    title: '指令属主',
                }),
                new TableColumn({
                    prop: 'total',
                    title: '统计',
                    customComponent: true,
                }),
                new TableColumn({
                    prop: 'operationType',
                    title: '操作类型',
                    formatter: this.operationTypeFormat,
                }),
                new TableColumn({
                    prop: 'reportStatus',
                    title: '报备阶段',
                    formatter: TableColumn.reportStatusFormat,
                }),
                new TableColumn({
                    prop: 'timeStamp',
                    title: '生成时间',
                    formatter: TableColumn.datetimeFormat,
                }),
                new TableColumn({
                    prop: 'operating',
                    title: '操作',
                    width: 100,
                    customComponent: 'true',
                }),
            ],
            houses: [],
            insFilterRules: [],
            dialogTableVisible: false,
            title: '',
            operationTypes:{
                0:"新增",
                1:"删除",
            },
            subtypes:{
                1:"域名",
                2:"URL",
                3:"关键字",
                4:"源IP地址",
                5:"目的IP地址",
                6:"源端口",
                7:"目的端口",
                8:"应用协议",
            },
            keywordRangeMap: new Map().set(0, "正文标题及正文本身").set(1, "附件文件题目").set(2, "附件正文")
        }
    },
    methods: {
        createForm() {
            return new QueryEntity();
        },
        logFormat(row, column, value, index) {
            return logMap.get(parseInt(value));
        },
        reportFormat(row, column, value, index) {
            return reportMap.get(parseInt(value));
        },
        levelFormat(row, column, value, index) {
            return parseInt(value, 2);
        },
        operationTypeFormat(row, column, value, index) {
            return operationTypeMap.get(parseInt(value));
        },
        insSourceFormat(row, column, value, index) {
            return insSourceMap.get(parseInt(value));
        },
        showWindow(data) {
            const cloneDeep = _.cloneDeep(data);
            this.title = '过滤指令ID为' + cloneDeep.id + '的规则';
            this.insFilterRules = cloneDeep.insFilterRules.map(data => {
                //规则类型为传输层协议
                if (parseInt(data.subtype) === 8) {
                    if (parseInt(data.valueStart) === 1) {
                        data.valueStart = 'TCP';
                    } else {
                        data.valueStart = 'UDP';
                    }
                } else if (parseInt(data.subtype) === 3) {
                    const strings = data.keywordRange.split(",");
                    const dataArray = strings.map(data => {
                        return this.keywordRangeMap.get(parseInt(data));
                    })
                    data.keywordRange = dataArray.join(",");
                }
                return data;
            });
            this.dialogTableVisible = true;

        },
        updateForward(data) {
            forward('/command-manage/modify/admin_commandfilter_modify', data);
        },
        async deleteRow(data) {
            var res = await rxPrompt.confirm('是否确认要删除指令吗？', ['确认', '取消'])
            if (res) {
                var array = new Array();
                array.push(data)
                const result = await insFilterApi.deleteBatch(array);
                if (result.code !== 200 || result.data != true) {
                    await rxPrompt.msg(`删除失败,${result.msg}`)
                    return
                }
                await rxPrompt.msg('删除成功')
                this.page.current = 1
                await this.searchPage()
            }
        },
        init() {
            this.initHouses();
        },
        async initHouses() {
            this.houses = await baseHouseInfoApi.selectList();
        },
        subTypeFormat(row, column, cellValue, index) {
            return subTypeFormat.get(parseInt(cellValue));
        },
        selection: function (data) {
            this.selectedIdList = data;
        },
        deleteSelected: async function () {
            if (this.selectedIdList.length <= 0) {
                await rxPrompt.msg('请选择数据')
                return
            }
            const res = await rxPrompt.confirm('你确定要删除吗？', ['确认', '取消'])
            if (!res) {
                return
            }
            if (this.selectedIdList
                .find(({insSource}) => Number.isInteger(insSource) && parseInt(insSource) === 1)) {
                await rxPrompt.msg('不能删除指令源为SMMS的指令,请重新选择')
                return
            }
            const result = await insFilterApi.deleteBatch(this.selectedIdList)
            if (result.code !== 200 || result.data != true) {
                await rxPrompt.msg(`删除失败,${result.msg}`)
                return
            }
            await rxPrompt.msg('删除成功')
            this.page.current = 1
            await this.searchPage()
        },
        timeArrayChange(array) {
            [start, end] = array || [];
            this.form.startTime = start;
            this.form.endTime = end;
        },
        updateShow(data) {
            //加2分钟 120秒==2分钟
            if (rx.dateBetween(new Date(data.effectTime), new Date()).second() + 120 >= 0) {
                return false;
            }
            return true;
        }
    },
    computed: {},
    watch: {
        'form.timeArray': function (newVal, oldVal) {
            if (newVal) {
                this.form.startTime = newVal[0];
                this.form.endTime = newVal[1];
            } else {
                this.form.startTime = '';
                this.form.endTime = '';
            }

        },
    },
})
